<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<input type="text" />
<input type="button" value="平方" onclick="f()"/>
<h3>
    结果：<span></span>
</h3>
</body>
<script>
    let i1 = document.querySelector("input");
    let s = document.querySelector("span");
    function f(){
        /*let num = i1.value*1;*/

        //字符串在进行-*/运算时会自动转成数值类型
        // s.innerText = i.value*i.value;
        //把字符串转成数值 后再进行加法运算
        // s.innerText = parseFloat(i.value)+parseFloat(i.value);
        let num = parseFloat(i1.value);
        /*alert(typeof num);
        if(typeof(num)=='number'){
            alert("正确");
        }*/
        //判断输入的内容是否是数值
        if(isNaN(num)){
            s.innerText = "输入错误";
            return;
        }
        s.innerText = num+num;
    }

</script>
</html>